<?PHP
if (!session_start()) { session_start(); };
/*if (!isset($_SESSION['lgnapproved'])) {
	die(header('Location: login.php'));
}*/

include("functions.php");
include("cnnxn.php");
connect();

if (isset($_POST['submit'])) {
	include("simpleresize.php");

	$dateadded=date("Y-m-d");
	
	$fieldnames="";
	$tempvalue="";
	$modstring="";
	while (list($key,$value) = each($_POST)) {
	if ($value!='modify') {
		if (ctype_digit($value)) {
		   $tempvalue=$value;
		} else {
		   $tempvalue='"'.addslashes($value).'"';
		}
		$modstring=$modstring.$key.'='.$tempvalue.',';
	}
	}
	
	if (isset($_FILES['Picture']) && $_FILES['Picture']['name']!='') {
		$SQLgoi='SELECT * FROM Players WHERE PlayerID='.$_GET['id'].';';
		$sendgoi=mysqli_query($db,$SQLgoi) or die(mysqli_error($db));
		$RSgoi=mysqli_fetch_object($sendgoi);
		
		if (file_exists('../images/players/'.$RSgoi->Picture)) {		
			unlink('../images/players/'.$RSgoi->Picture);
		}
		
		$image = new SimpleImage();
		$image->load($_FILES['Picture']['tmp_name']);	
		list($width, $height) = getimagesize($_FILES['Picture']['tmp_name']);
		$setWidth=200;
		$setHeight=200;
		if ($width>=$height) {
			if ($setWidth<=$width) {
				$image->resizeToWidth($setWidth);
			}
		} else {
			if ($setHeight<=$height) {
				$image->resizeToHeight($setHeight);
			}
		}
		$image->save('../images/players/'.$_FILES['Picture']['name']);
		$modstring=$modstring.'Picture="'.$_FILES['Picture']['name'].'",';
	}
	
	//$fieldnames=substr_replace($fieldnames,"",-8);
	$modstring=substr_replace($modstring,"",-1);
	$SQLmod='UPDATE Players SET '.$modstring.' WHERE PlayerID='.$_GET['id'].';';
	//print $SQLmod;
	$sendit_mod=mysqli_query($db,$SQLmod) or die(mysqli_error($db));
	die(header("Location: Players_viewall.php"));
} else {
	$SQLdet='SELECT * FROM Players WHERE PlayerID='.$_GET['id'].';';
	//print $SQLdet;
	$sendit_det=mysqli_query($db,$SQLdet) or die(mysqli_error($db));
	$RSdet=mysqli_fetch_object($sendit_det) or die(mysqli_error($db));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>daleclark.net</title>
<link type="text/css" rel="stylesheet" href="adminstyles.css" />
</head>
<body>
<div id="adminwrapper">
<div id="adminlogo"></div>
<?PHP navigation(); ?>
<div id="admincontent">
<h3>Modify or Delete Players</h3>
<form name="addForm" method="POST" action="Players_mod.php?id=<?PHP print $_GET['id']; ?>" enctype="multipart/form-data">
<p><span class="label">First Name</span><span class="inputbox"><input name="FirstName" type="text" value="<?PHP print $RSdet->FirstName; ?>" /></span></p>
<p><span class="label">Last Name</span><span class="inputbox"><input name="LastName" type="text" value="<?PHP print $RSdet->LastName; ?>" /></span></p>
<p><span class="label">Phone</span><span class="inputbox"><input name="Phone" type="text" value="<?PHP print $RSdet->Phone; ?>" /></span></p>
<p><span class="label">Email</span><span class="inputbox"><input name="Email" type="text" value="<?PHP print $RSdet->Email; ?>" /></span></p>
<p><span class="label">Gender</span><span class="inputbox"><select name="GenderID">
<?PHP 
$SQLgg='SELECT * FROM Gender;';
$sendgg=mysqli_query($db,$SQLgg) or die(mysqli_error($db));
while ($RSgg=mysqli_fetch_object($sendgg)) {
	print '<option value="'.$RSgg->GenderID.'"';
	if ($RSgg->GenderID==$RSdet->GenderID) {
		print ' selected';
	}
	print '>'.$RSgg->Gender.'</option>';
}
?>
</select></span></p>
<p><span class="label">Home Town</span><span class="inputbox"><input name="Hometown" type="text" value="<?PHP print $RSdet->Hometown; ?>" /></span></p>
<p><span class="label">Current Town</span><span class="inputbox"><input name="CurrentTown" type="text" value="<?PHP print $RSdet->CurrentTown; ?>" /></span></p>
<p><span class="label">Local Beach</span><span class="inputbox"><input name="LocalBeach" type="text" value="<?PHP print $RSdet->LocalBeach; ?>" /></span></p>
<p><span class="label">Height</span><span class="inputbox"><input name="Height" type="text" value="<?PHP print $RSdet->Height; ?>" /></span></p>
<p><span class="label">Picture</span><span class="inputbox"><input name="Picture" type="file" /><?PHP if ($RSdet->Picture!='') { ?><img src="../images/players/<?PHP print $RSdet->Picture; ?>" style="height:50px;" /><?PHP } ?></span></p>
<p><span class="label">College</span><span class="inputbox"><input name="College" type="text" value="<?PHP print $RSdet->College; ?>" /></span></p>
<p><span class="label">Is AVA Volunteer</span><span class="inputbox"><input name="IsAVAVolunteer" type="checkbox" value="1"<?PHP if ($RSdet->IsAVAVolunteer==1) print " checked" ?> /></span></p>
<p><span class="label">Current Played</span><span class="inputbox"><input name="CurrentPlayed" type="text" value="<?PHP print $RSdet->CurrentPlayed; ?>" /></span></p>
<p><span class="label">Current Points</span><span class="inputbox"><input name="CurrentPoints" type="text" value="<?PHP print $RSdet->CurrentPoints; ?>" /></span></p>
<p><span class="label">Current Unadjusted Rating</span><span class="inputbox"><input name="CurrentUnadjustedRating" type="text" value="<?PHP print $RSdet->CurrentUnadjustedRating; ?>" /></span></p>
<p><span class="label">Current Unadjusted Rating Date</span><span class="inputbox"><input name="CurrentUnadjustedRatingDate" type="text" value="<?PHP print $RSdet->CurrentUnadjustedRatingDate; ?>" /></span></p>
<p><input type="submit" value="modify" name="submit" /></p></form>
<p><a href="Players_del.php?id=<?PHP print $_GET['id']; ?>" class="deletelink">DELETE RECORD</a></p></div>
</div>
</body>
</html>
<?PHP
mysqli_close($db);
?>